package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.WorkOrder;
import cn.stylefeng.guns.sys.modular.system.entity.WorkOrderShenpi;
import cn.stylefeng.guns.sys.modular.system.mapper.WorkOrderShenpiMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.WorkOrderShenpiParam;
import cn.stylefeng.guns.sys.modular.system.model.result.WorkOrderShenpiResult;
import cn.stylefeng.guns.sys.modular.system.service.WorkOrderService;
import cn.stylefeng.guns.sys.modular.system.service.workOrderShenpiService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;

@Service
public class workOrderShenpiServiceImpl extends ServiceImpl<WorkOrderShenpiMapper, WorkOrderShenpi> implements workOrderShenpiService {

    @Autowired
    private WorkOrderService workOrderService;

    @Override
    public ResponseData add(WorkOrderShenpiParam workOrderShenpiParam) {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData update(WorkOrderShenpiParam param) {
        WorkOrderShenpi oldEntity = getOldEntity(param);
        WorkOrderShenpi newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);

        //新增子表数据
        newEntity.setSta(2);//已回复
        newEntity.setWorkId(param.getId());
        newEntity.setCreateUser(LoginContextHolder.getContext().getUserId());
        newEntity.setCreateTime(new Date());
        baseMapper.insert(newEntity);

        //修改主表数据
        WorkOrder workOrder = new WorkOrder();
        workOrder.setSta(2);//已回复
        workOrder.setId(param.getId());
        workOrderService.updateById(workOrder);
        return ResponseData.success();
    }

    @Override
    public void delete(WorkOrderShenpiParam workOrderShenpiParam) {

    }

    @Override
    public LayuiPageInfo findPageBySpec(WorkOrderShenpiParam param) {
        Page pageContext = getPageContext();
        param.setCreateUser(LoginContextHolder.getContext().getUserId());
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public WorkOrderShenpiResult detail(Long id) {
        WorkOrderShenpiResult shenpi = baseMapper.detail(id);
        return shenpi;
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private WorkOrderShenpi getOldEntity(WorkOrderShenpiParam param) {
        return this.baseMapper.getByWorkId(getKey(param));
    }

    private Serializable getKey(WorkOrderShenpiParam param) {
        return param.getId();
    }

    private WorkOrderShenpi getEntity(WorkOrderShenpiParam param) {
        WorkOrderShenpi entity = new WorkOrderShenpi();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }
}
